import express, { Express } from 'express';
import cors from 'cors';
import terminalRoutes from './routes/terminals';
import { logger } from './middleware/logger';
import { errorHandler, notFoundHandler } from './middleware/errorHandler';

export const createServer = (): Express => {
  const app = express();

  app.use(cors({
    origin: process.env.CORS_ORIGIN || '*',
    credentials: true
  }));

  app.use(express.json());
  app.use(express.urlencoded({ extended: true }));

  app.use(logger);

  app.use('/api', terminalRoutes);

  app.use(notFoundHandler);
  app.use(errorHandler);

  return app;
};
